package database;

import main.Main;
import main.SettingsHandler;
import tools.StringTools;

public class DBConnectionFactory {
   private static ConnectionHandler CONNECTION_HANDLER;

   public static final ConnectionHandler getConnectionHandler(final boolean... renew) {
	  if (CONNECTION_HANDLER == null || renew.length == 1 && renew[0]) {
		 if (Main.getBotSettings().useEmbeddedDB()) {
			CONNECTION_HANDLER = new HSQLDBConnectionHandler(new SettingsHandler().loadDBSettings());
		 } else {
			CONNECTION_HANDLER = new MySQLConnectionHandler(new SettingsHandler().loadDBSettings());
		 }
	  }
	  return CONNECTION_HANDLER;
   }

   public static final ConnectionHandler getNewConnectionHandler(final String dbHost, final String dbName, final String dbUser,
		 final String dbPassword) {
	  if ("embedded".equalsIgnoreCase(dbHost))
		 return new HSQLDBConnectionHandler(dbHost, dbName, dbUser, dbPassword);
	  else if (!StringTools.isNullOrEmpty(dbHost))
		 return new MySQLConnectionHandler(dbHost, dbName, dbUser, dbPassword);
	  return null;
   }

   public static final InstallHandler getNewInstallHandler(final String dbHost, final String dbName, final String dbUser,
		 final String dbPassword) {
	  if (dbHost.equalsIgnoreCase("embedded"))
		 return new HSQLDBInstallHandler(dbHost, dbName, dbUser, dbPassword);
	  else
		 return new MySQLInstallHandler(dbHost, dbName, dbUser, dbPassword);
   }
}
